package com.yinhai.project.config;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.yinhai.modules.authority.api.bpo.IAuthorityBpo;
import com.yinhai.modules.authority.api.vo.MenuVo;
import com.yinhai.modules.cache.api.cache.ICache;
import com.yinhai.modules.cache.api.manager.ICacheManager;

/**
 * 项目启动的时候，缓存全部的岗位的权限
 * @author maxp  2016年8月23日-下午5:32:44
 */
public class CachePositionsAuthorities implements InitializingBean {
	
	private static Log logger = LogFactory.getLog(CachePositionsAuthorities.class);
	
	@Resource(name="cacheDepot")
    private ICacheManager cacheDepot;
	
	@Resource(name="authorityBpo")
	private IAuthorityBpo authorityBpo;
	
	/**
	 * 缓存全部岗位权限的方法
	 * @author maxp 2016年8月23日-下午6:58:03
	 */
	public void initCachePosiAuth(){
		logger.info("开始缓存全部岗位权限");
		ICache cache = cacheDepot.getCache("posiAuthCache"); 
		Map<Long,Map<String,List<MenuVo>>> maps= authorityBpo.getAllMenusOfAllPositionsOnStartUp();
		for(Long l:maps.keySet()){
			logger.info("缓存岗位权限: 岗位id"+l);
			cache.put(l, maps.get(l));
		}
		logger.info("缓存全部岗位结束");
	}


	@Override
	public void afterPropertiesSet() throws Exception {
		initCachePosiAuth();
	}

	
}
